<?
/**
 * Copyright 2007 Melange.
 *
 * This file is part of PHP-MELANGE.
 *
 * PHP-MELANGE is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * PHP-MELANGE is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with PHP-MELANGE; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * @category    Melange
 * @package     php-melange
 * @subpackage  cms
 * @copyright   Copyright (c) 2007 Jeroen Simons. All rights reserved
 * @author      Jeroen Simons <jeroen@melange.nl>
 * @link        http://www.melange.nl/
 *
 */

require_once("mvc/action/Action.php");
require_once("cms/menu/om/MenuItemI18nPeer.php");

class EditAction extends Action {

    private $log;

    public function __construct() {
        $this->log =& LoggerManager::getLogger("EditAction");
    }

    public function execute(ActionConfig &$mapping, &$form, Request &$request, Response &$response) {

        if($this->log->isDebugEnabled())
            $this->log->debug("Executing item/EditAction");

        if($form->getItemI18n()->getMenuItemI18nId() > 0) {
            $itemI18n = MenuItemI18nPeer::retrieveByPk($form->getItemI18n()->getMenuItemI18nId());
            if(!is_null($itemI18n)) {
                $form->setItemI18n($itemI18n);

                // Set the internal link if needed.
                if(strpos($itemI18n->getMenuItem()->getLink(), "http") === false && strpos($itemI18n->getMenuItem()->getLink(), "https") === false) {

                    // Using the category table.
                    if(!is_null($itemI18n->getMenuItem()->getCategoryId()) && $itemI18n->getMenuItem()->getCategoryId() > 0) {

                        $form->setModule($itemI18n->getMenuItem()->getCategoryId());
                        $form->setInternalLink($itemI18n->getMenuItem()->getLink());
                        $form->getItemI18n()->getMenuItem()->setLink("");

                    // Using the internal without category table.
                    } else {

                        $module = strpos($itemI18n->getMenuItem()->getLink(), "/") !== false ?
                            substr($itemI18n->getMenuItem()->getLink(), 0, strpos($itemI18n->getMenuItem()->getLink(), "/")) :
                            $itemI18n->getMenuItem()->getLink();

                        $link = substr($itemI18n->getMenuItem()->getLink(), (strpos($itemI18n->getMenuItem()->getLink(), "/") + 1));

                        $form->setModule($module);
                        $form->setInternalLink($link);

                    }

                    $form->getItemI18n()->getMenuItem()->setLink("");
                }

            } else {
                $this->log->error("No menuItemI18n found with id ".$form->getItemI18n()->getMenuItemI18nId().".");
                $form->getItemI18n()->setMenuItemI18nId("");
            }
        } else {

            if($form->getItemI18n()->getLanguageCode() === null)
                $form->getItemI18n()->setLanguageCode(
                    $request->getContext()->getInitParameter(_Core::DEFAULT_LANGUAGE)
                );

        }

        return $mapping->findForward("success");

    }

}
?>